package self.hoy.demo.grainmall.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.grainmall.product.mapper.BrandMapper;
import self.hoy.demo.grainmall.product.entity.Brand;
import self.hoy.demo.grainmall.product.service.BrandService;
import self.hoy.demo.grainmall.product.service.CategoryBrandRelationService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private CategoryBrandRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("brand_id", key).or().like("name", key);
        }

        IPage<Brand> page = this.page(
                new Query<Brand>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateDetails(Brand brand) {
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            relationService.updateBrand(brand.getBrandId(), brand.getName());
        }
    }

}